
package us.gaaoc.framework.webapp.backingbeans.admin;

import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.component.UISelectItem;
import javax.faces.component.html.HtmlSelectOneListbox;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.util.DataPage;
import us.gaaoc.framework.model.util.FilingSorter;
import us.gaaoc.framework.model.util.PagedListDataModel;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.backingbeans.caseTypes.CaseRecordType;
import us.gaaoc.framework.webapp.backingbeans.profile.Links;

/**
 * @author tapshals
 * 
 */
public class MainPage extends BaseBackingBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Properties
	 */
	private static final Logger LOG = Logger.getLogger(MainPage.class);
	private int newFilingCount;	
	private int transmissionErrorCount;
	private HtmlSelectOneListbox  caseTypeUI;
	private Filing filing;
	private UIParameter caseDetailsPageClicked;
	private DataModel dataTableList;
	
	private List<Filing> newReceivedFilings;
	
	// Variables Used for Sort functionality	
	private FilingSorter filingSorter_Received;
	
	// Variables Used for Paging Imported Filings
	private int receivedFilingsRowCount;
	
	private String courtUuid;
	
	/**
	 * Constructor
	 *
	 */
	public MainPage() {
		courtUuid = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		
		this.caseTypeUI = new HtmlSelectOneListbox();
		CaseRecordType caseRecordType = new CaseRecordType();
		for (Links links : caseRecordType.getMenu()) {
			SelectItem item = new SelectItem(links.getNavigationRule(), links.getText());
			UISelectItem uiItem = new UISelectItem();
			uiItem.setValue(item);

			this.caseTypeUI.getChildren().add(uiItem);
		}
		// New filing commented out on 2007-07-23
		// Is this used any more? Would be good to be used as a display item
		// to inform the Court Clerk of the number of new filings, but didn't
		// find it on the page.  Commenting out to help page load faster until it
		// is used again.
		List cr = this.getModelFacade().findAllCaseRecordByStatus(1, courtUuid);		 
		this.setNewFilingCount(cr.size());
		List  cr2 = this.getModelFacade().findErrorFilings();
		this.setTransmissionErrorCount(cr2.size());
		
		filingSorter_Received = new FilingSorter("eventDateTime",this.getNewReceivedFilings());
		//dataTableList = new ListDataModel();
		//this.loadData();
	}

	/**
	 * Getter for property caseTypeUI
	 * @return
	 */
	public HtmlSelectOneListbox getCaseTypeUI() {
		return caseTypeUI;
	}
	
	/**
	 * Setter for property caseTypeUI
	 * @param caseTypeUI
	 */
	public void setCaseTypeUI(HtmlSelectOneListbox caseTypeUI) {
		this.caseTypeUI = caseTypeUI;
	}
	/**
	 * Setter for property newFilingCount
	 * @param newFilingCount
	 */
	public void setNewFilingCount(int newFilingCount){
		this.newFilingCount = newFilingCount;
	}
	
	/**
	 * Getter for property newFilingCount
	 * @return newFilingCount
	 */
	public int getNewFilingCount() {
		return this.newFilingCount;
	}
	/**
	 * Setter for property transmissionErrorCount
	 * @param transmissionErrorCount
	 */
	public void setTransmissionErrorCount(int transmissionErrorCount) {
		this.transmissionErrorCount = transmissionErrorCount;
	}
	
	/**
	 * Getter for property transmissionErrorCount
	 * @return transmissionErrorCount
	 */
	public int getTransmissionErrorCount() {
		return this.transmissionErrorCount;
	}
	
	/**
	 * This method navigates user to corresponding page depending on the status
	 * of case record
	 * @return String
	 */
	public String submitAction() {
		String navigationString = "";
		if(this.getCaseTypeUI().getValue().equals(null))
			navigationString = "";
		else if (this.getCaseTypeUI().getValue().equals("appellate"))
			navigationString = "appellate";
		else if (this.getCaseTypeUI().getValue().equals("civil"))
			navigationString = "civil";
		else if (this.getCaseTypeUI().getValue().equals("childEnforcement"))
			navigationString = "gotoChildEnforcementCaseTypePage";
		return navigationString;
	}
	
	/**
	 * This method navigates user to the Transmission Error Filing Page
	 * @return String
	 */
	public String gotoTransmissionErrorFilingPage(){		 
		return "transmissionErrorFilingPage";
	}
	
	/**
	 * This method user to the New Filing Page
	 * @return String
	 */
	public String gotoNewFiliingsPage() {
		return "newFilingPage";
	}
	
	/**
	 * This method loads the data from database of staus="Received"
	 *
	 */
	private void loadData(){
        //   status = received (1)
		List<Filing> newFilings = this.getModelFacade().findCaseFilingsByStatus(1, courtUuid);			
		dataTableList.setWrappedData(newFilings);
	}

	/**
	 * @return Returns the dataTableList.
	 */
	public DataModel getDataTableList() {
		dataTableList = new ReceivedStatusFilingsDataModel(10);
		return dataTableList;
	}

	/**
	 * @param dataTableList The dataTableList to set.
	 */
	public void setDataTableList(DataModel dataTableList) {
		this.dataTableList = dataTableList;
	}
	
	/**
	 * This method navigates user to the Case Details Page
	 * @return String
	 */
	public String gotoCaseDetailPage(){
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
	 	
	 	this.setFiling ( (Filing)this.getCaseDetailsPageClicked().getValue());
	 	String caseType = filing.getCaseType();
	 	String toString="";
	 	if((caseType.equals("ChildSupportEnforcement")) || (caseType.equalsIgnoreCase("Superior Court Child Support")))
	 		toString = "gotoCSECaseDetailsPage";
	 	else toString = "gotoCaseDetailsPage";
	 	
	 	sessionMap.put("caseUuid", filing.getUuid());
	 	sessionMap.put("from", "MainPage");
	 	return toString;
	}
	

	/**
	 * @return Returns the caseDetailsPageClicked.
	 */
	public UIParameter getCaseDetailsPageClicked() {
		return caseDetailsPageClicked;
	}

	/**
	 * @param caseDetailsPageClicked The caseDetailsPageClicked to set.
	 */
	public void setCaseDetailsPageClicked(UIParameter caseDetailsPageClicked) {
		this.caseDetailsPageClicked = caseDetailsPageClicked;
	}

	/**
	 * @return Returns the filing.
	 */
	public Filing getFiling() {
		return filing;
	}

	/**
	 * @param filing The filing to set.
	 */
	public void setFiling(Filing filing) {
		this.filing = filing;
	}

	/**
	 * @return the filingSorter_received
	 */
	public FilingSorter getFilingSorter_Received() {
		return filingSorter_Received;
	}

	/**
	 * @param filingSorter_received the filingSorter_received to set
	 */
	public void setFilingSorter_Received(FilingSorter filingSorter_Received) {
		this.filingSorter_Received = filingSorter_Received;
	}

	/**
	 * @return the newReceivedFilings
	 */
	public List<Filing> getNewReceivedFilings() {
		return newReceivedFilings;
	}

	/**
	 * @param newReceivedFilings the newReceivedFilings to set
	 */
	public void setNewReceivedFilings(List<Filing> newReceivedFilings) {
		this.newReceivedFilings = newReceivedFilings;
	}

	/**
	 * @return the receivedFilingsRowCount
	 */
	public int getReceivedFilingsRowCount() {
		receivedFilingsRowCount = this.getModelFacade().findNumberOfCaseFilingsByTypeAndStatus(1, 1, courtUuid);
		return receivedFilingsRowCount;
	}

	/**
	 * @param receivedFilingsRowCount the receivedFilingsRowCount to set
	 */
	public void setReceivedFilingsRowCount(int receivedFilingsRowCount) {
		this.receivedFilingsRowCount = receivedFilingsRowCount;
	}
	
	/**
	 * Method used for paging of large data set with Data Page class
	 */
	private DataPage<Filing> getReceivedFilingsDataPage(int startRow, int pageSize){
		int dataSetSize = getReceivedFilingsRowCount();
		if(dataSetSize  >= 10) {
			List<Filing> rFilings = this.getModelFacade().findCaseFilingsByTypeAndStatusPaged(1, 1, startRow, pageSize, courtUuid);
			DataPage<Filing> receivedDataTableListForPage = new DataPage<Filing>(dataSetSize, startRow, rFilings);
			filingSorter_Received.sort("eventDateTime", filingSorter_Received.isAscending(),rFilings);
			return receivedDataTableListForPage;
		}
		else {
			List<Filing> rFilings = this.getModelFacade().findCaseFilingsByTypeAndStatus(1, 1, courtUuid);
			DataPage<Filing> receivedDataTableListForPage = new DataPage<Filing>(dataSetSize, startRow, rFilings);
			filingSorter_Received.sort("eventDateTime", filingSorter_Received.isAscending(),rFilings);
			return receivedDataTableListForPage;
		}
	}
	
	/**
	 * Internal Class used for paging of Imported Status Filings Data
	 * 
	 * @author mcelratr
	 *
	 */
	private class ReceivedStatusFilingsDataModel extends PagedListDataModel {
		public ReceivedStatusFilingsDataModel(int pageSize) {
            super(pageSize);
        }
        
        public DataPage<Filing> fetchPage(int startRow, int pageSize) {
            // call enclosing managed bean method to fetch the data
        	return MainPage.this.getReceivedFilingsDataPage(startRow, pageSize);
        }
    }
	
	public String goToAllCaseRecordsByCaseTypeAndStatus(){
		System.out.println("Inside the goToAllCaseRecordsByCaseTypeAndStatus method");
		return "goToAllCaseRecordsByCaseTypeAndStatusPage";
	}
	

}